package com.lmwyc.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lmwyc.bean.AreaDo;
import com.lmwyc.mapper.AreaMapper;
import com.lmwyc.service.IAreaService;

@Service
public class AreaServiceImpl implements IAreaService {
	private static final Logger logger = LoggerFactory.getLogger(AreaServiceImpl.class);
	
//	private List<Area> areas;

	@Autowired
	private AreaMapper areaMapper;

//	@Override
//	public synchronized void setAreas(List<Area> areas) {
//		this.areas = areas;
//	}

//	@Override
//	public int saveAreas() {
//		if (areas == null || areas.size() < 1) {
//			logger.error("**********************************没有有效的区域列表！！！**********************************");
//			return 0;
//		}
//		areaMapper.truncate();
//		int cnt = 0;
//		List<AreaDo> temps = new ArrayList<>();
//		for (Area area : areas) {
//			AreaDo areaDo = new AreaDo();
//			areaDo.setId(area.getId());
//			areaDo.setOnwership_place(area.getOnwership_place());
//			areaDo.setOffice(area.getOffice());
//			try {
//				areaDo.setLongitude(Double.parseDouble(area.getLongitude()));
//				areaDo.setLatitude(Double.parseDouble(area.getLatitude()));
//			}catch (Exception e) {
//				logger.error("区域列表异常数据>" + area);
//				continue;
//			}
//			areaDo.setShip_place_ind(area.getShipPlaceInd());
//			temps.add(areaDo);
//			cnt++;
//			if (temps.size() == 50) {
//				areaMapper.save(temps);
//				temps.clear();
//			}
//		}
//		if (temps.size() > 0) {
//			areaMapper.save(temps);
//		}
//		return cnt;
//	}

	@Override
	public List<AreaDo> list(Integer id, String onwershipPlace, String office, double lat, double lon) {
		if(lat == 0 || lon == 0) {
			return areaMapper.list(id, onwershipPlace, office);
		}
		List<AreaDo> temp = areaMapper.list(id, onwershipPlace, office);
		List<AreaDo> areas = new ArrayList<AreaDo>();
		for(AreaDo area : temp) {
			double dis = computeDistance(lat, lon, area);
			if(dis < area.getShip_place_ind()) {
				areas.add(area);
			}
		}
		return areas;
	}
	
	/**
	 * 根据坐标值计算到中心点的距离
	 * 
	 * @param latitude
	 * @param longitude
	 * @param area
	 * @return
	 */
	private double computeDistance(double latitude, double longitude, AreaDo area) {
		// 打卡位置经纬度转换为度
		double cur_lat_angle = degreesToRadians(latitude);
		double cur_lob_angle = degreesToRadians(longitude);
		// 中心点经纬度转换为度
		double center_lat_angle = 0;
		double center_lob_angle = 0;
		try {
			// 区域列表中有异常数据
			center_lat_angle = degreesToRadians(Double.valueOf(area.getLatitude()));
			center_lob_angle = degreesToRadians(Double.valueOf(area.getLongitude()));
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("区域异常数据>" + area);
		}

		return 6371000 * Math.acos(Math.sin(cur_lat_angle) * Math.sin(center_lat_angle)
				+ Math.cos(cur_lat_angle) * Math.cos(center_lat_angle) * Math.cos(cur_lob_angle - center_lob_angle));
	}
	
	/**
	 * 将角度转换为弧度
	 * 
	 * @param axe
	 * @return
	 */
	private double degreesToRadians(Double axe) {
		return axe * Math.PI / 180;
	}
}
